package com.seqb.manage.menu.controller;

import com.github.pagehelper.Page;
import com.seqb.annotation.Log;
import com.seqb.common.ResultVo;
import com.seqb.common.controller.BaseController;
import com.seqb.manage.menu.domain.MenuDO;
import com.seqb.manage.menu.service.MenuService;
import com.seqb.utils.PageUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author lilinshen
 * @title 菜单表
 * @description 请填写相关描述
 * @date 2019/11/15
 */
@Controller
@RequestMapping("/manage/menu/menu")
public class MenuController extends BaseController {
    @Autowired
    private MenuService menuService;

    @GetMapping()
    @RequiresPermissions("manage:menu:menu:list")
    @Log("菜单表列表")
    String menu() {
        return "manage/menu/menu.list";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("manage:menu:menu:list")
    @Log("菜单表列表")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Page<MenuDO> page = menuService.list(params);
        PageUtils pageUtils = new PageUtils(page.getResult(), page.getTotal());
        return pageUtils;
    }

    @GetMapping("/add/{parentId}")
    @RequiresPermissions("manage:menu:menu:add")
    @Log("菜单表添加")
    String add(Model model, @PathVariable("parentId") Long parentId) {
        model.addAttribute("parentId", parentId);
        if (parentId == 0) {
            model.addAttribute("parentName", "根目录");
        } else {
            model.addAttribute("parentName", menuService.get(parentId).getName());
        }
        return "manage/menu/menu.add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("manage:menu:menu:edit")
    @Log("菜单表编辑")
    String edit(@PathVariable("id") Long id, Model model) {
        MenuDO menu = menuService.get(id);
        Long parentId = menu.getParentId();
        model.addAttribute("parentId", parentId);
        if (parentId == 0) {
            model.addAttribute("parentName", "根目录");
        } else {
            model.addAttribute("parentName", menuService.get(parentId).getName());
        }
        model.addAttribute("menu", menu);
        return "manage/menu/menu.edit";
    }

    /**
     * 添加保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("manage:menu:menu:add")
    @Log("菜单表添加保存")
    public ResultVo save(MenuDO menu) {
        if (menuService.save(menu) > 0) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 编辑保存
     */
    @ResponseBody
    @RequestMapping("/update")
    @Log("菜单表编辑保存")
    @RequiresPermissions("manage:menu:menu:edit")
    public ResultVo update(MenuDO menu) {
        menuService.update(menu);
        return ResultVo.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("manage:menu:menu:remove")
    @Log("菜单表删除")
    public ResultVo remove(Long id) {
        if (menuService.remove(id) > 0) {
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("manage:menu:menu:remove")
    @Log("菜单表批量删除")
    public ResultVo remove(@RequestParam("ids[]") Long[] ids) {
        menuService.batchRemove(ids);
        return ResultVo.ok();
    }

    /**
     * 菜单树列表
     */
    @ResponseBody
    @RequestMapping("/menuTree")
    @Log("菜单树列表")
    public PageUtils menuTree(Long id) {
        //查询列表数据
        return menuService.menuTree(id);
    }

}
